package com.apes.fn.scm.retailOrder.repository;

import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author HuJinalin
 * @create 2018-10-31 16:07
 */
public interface RetailOrderRepository extends CustomRepository<RetailOrder, String> {
    RetailOrder findByOrigin(String origin);
    RetailOrder findByOriginOrSourceAndOrderTypeNotIn(String origin,String source,List<String> orderTypes);

    List<RetailOrder> findAllBySourceAndSceneId(String source, String sceneId);

    @Query(value = "select r from RetailOrder r where r.postDate >= :startDate and r.postDate <:endDate")
    List<RetailOrder> findAllByPostDate(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    @Query(value = "select r from RetailOrder r where r.createDate >= :startDate and r.createDate <:endDate")
    List<RetailOrder> findAllByCreateDate(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    @Query("SELECT T FROM RetailOrder T WHERE T.id = :id AND T.state IN ('done', 'vouched')")
    RetailOrder findRetailOrder(@Param("id") String id);

    RetailOrder findBySource(String source);

    @Query("SELECT R as retailOrder,L as lossQuotation,C as carAccident FROM RetailOrder R , LossQuotation L , CarAccident C WHERE R.source = L.id AND C.id = L.carAccident " +
            "AND R.createDate > :startDate AND R.createDate < :endDate")
    List<Map> findLossQuotationReport(@Param("startDate")Date startDate ,@Param("endDate") Date endDate);

    @Query(value = "SELECT ID, AGGREGATE_ID FROM Outbox_Event_Bac_221212_wq ", nativeQuery = true)
    Page<Map<String, String>> findAllForTest(Pageable pageable);

}
